<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
    <style>
    html {
        box-sizing: border-box;
    }
    *, *:before, *:after {
        box-sizing: inherit;
    }
    body {
        text-align: center;
        background: #003300;
        font-family: Arial;

    }
    #display {
        display:inline-block;
        border-radius: 5px;
        width: auto;
        margin: auto;
        padding: 20px;
        background: #00AA00;
        margin-bottom: 20px;
        box-shadow: 2px 2px 1px #99cc99;

    }
    #display.low_brightness .led {
        filter: brightness(0.6);
    }
    .led {
        background-color: #000;
        border: 1px solid #eee;
        display: inline-block;
        width: 20px;
        height: 20px;
        border-radius: 3px;
        margin: 1px;
        box-shadow: 1px 1px 1px #666;
    }
    #controls {
    }
    #environment {
        display: inline-block;
        vertical-align: middle;
    }
    #joystick {
        margin-left: 10px;
        vertical-align: middle;
        display: inline-block;
        margin-top: 20px;
    }
    .envvar {
        margin: 10px auto;
    }
    .envvar>span {
        display: block;
        color: silver;
        margin-bottom: 2px;
    }
    .envvar>div {
        border: 1px solid #999;
        border-radius: 3px;
        display:inline-block;
        font-size: 18px;
        background: #000;
        box-shadow: 1px 1px 1px #eee;

    }

    a {
        font-size: 38px;
        line-height: 40px;
        display: inline-block;
        width: 40px;
        height: 40px;
        background: silver;
        text-decoration: none;
        vertical-align: top;
        color: #333;
    }

    .envvar a {
        margin: none;
    }
    .envvar a:first-of-type {
        line-height: 35px;
    }

    a:active {
        background: grey;
    }

    .envvar .value {
        vertical-align: top;
        height: 40px;
        line-height: 40px;
        color: silver;
        display: inline-block;
        width: 80px;
        background: #000;
        box-shadow: inset 1px 1px 2px #ddd;

    }

    #joystick a {
        display: inline-block;
        width: 43px;
        height: 43px;
        border: 1px solid #999;
        margin: 3px;
        line-height: 50px;
        border-radius: 8px;
        box-shadow: 1px 1px 1px #eee;

    }
    #joystick a.up {}
    #joystick a.right {
        box-shadow: 1px -1px 1px #eee;
        transform: rotate(90deg);
    }
    #joystick a.down {
        box-shadow: -1px -1px 1px #eee;
        transform: rotate(180deg);
    }
    #joystick a.left {
        box-shadow: -1px 1px 1px #eee;
        transform: rotate(270deg);
    }
    #joystick a.enter {
    }

    </style>
</head>
<body>

<div id="display">
</div>
<div id="controls">
    <div id="environment">
        <div class="envvar">
            <span>Temperature</span>
            <div>
                <a href="" onclick="changeTemperature(-0.2); return false;">-</a><div class="value" id="temperature"></div><a href="" onclick="changeTemperature(0.2); return false;">+</a>
            </div>
        </div>
        <div class="envvar">
            <span>Humidity</span>
            <div>
                <a href="" onclick="changeHumidity(-1); return false;">-</a><div class="value" id="humidity"></div><a href="" onclick="changeHumidity(1); return false;">+</a>
            </div>
        </div>
        <div class="envvar">
            <span>Pressure</span>
            <div>
                <a href="" onclick="changePressure(-5); return false;">-</a><div class="value" id="pressure"></div><a href="" onclick="changePressure(5); return false;">+</a>
            </div>
        </div>
    </div>
    <div id="joystick">
        <div><a href="" id="j-up" class="up" onclick="return false;">^</a></div>
        <div><a href="" id="j-left" class="left" onclick="return false;">^</a><a id="j-enter" href="" class="enter" onclick="return false;"></a><a id="j-right" href="" class="right" onclick="return false;">^</a></div>
        <div><a href="" id="j-down" class="down" onclick="return false;">^</a></div>
    </div>
</div>

<script>

var fontData = {" ":[0,0,0,0,0,0,0,0],"+":[0,4,4,31,4,4,0,0],"-":[0,0,0,31,0,0,0,0],"*":[0,4,21,14,21,4,0,0],"/":[0,16,8,4,2,1,0,0],"!":[4,0,4,4,4,4,4,0],"\"":[0,0,0,0,10,10,10,0],"#":[10,10,31,10,31,10,10,0],"$":[4,30,5,14,20,15,4,0],">":[8,4,2,1,2,4,8,0],"<":[2,4,8,16,8,4,2,0],"0":[14,17,25,21,19,17,14,0],"1":[14,4,4,4,4,12,4,0],"2":[31,8,4,2,1,17,14,0],"3":[14,17,1,2,4,2,31,0],"4":[2,2,31,18,10,6,2,0],"5":[14,17,1,1,30,16,31,0],"6":[14,17,17,30,16,8,6,0],"7":[4,4,4,4,2,1,31,0],"8":[14,17,17,14,17,17,14,0],"9":[12,2,1,15,17,17,14,0],".":[12,12,0,0,0,0,0,0],"=":[0,0,31,0,31,0,0,0],")":[8,4,2,2,2,4,8,0],"(":[2,4,8,8,8,4,2,0],"A":[17,17,17,31,17,17,14,0],"B":[30,17,17,30,17,17,30,0],"C":[14,17,16,16,16,17,14,0],"D":[28,18,17,17,17,18,28,0],"E":[31,16,16,30,16,16,31,0],"F":[16,16,16,30,16,16,31,0],"G":[15,17,17,23,16,17,14,0],"H":[17,17,17,31,17,17,17,0],"I":[14,4,4,4,4,4,14,0],"J":[12,18,2,2,2,2,7,0],"K":[17,18,20,24,20,18,17,0],"L":[31,16,16,16,16,16,16,0],"M":[17,17,17,21,21,27,17,0],"N":[17,17,19,21,25,17,17,0],"O":[14,17,17,17,17,17,14,0],"P":[16,16,16,30,17,17,30,0],"Q":[13,18,21,17,17,17,14,0],"R":[17,18,20,30,17,17,30,0],"S":[30,1,1,14,16,16,15,0],"T":[4,4,4,4,4,4,31,0],"U":[14,17,17,17,17,17,17,0],"V":[4,10,17,17,17,17,17,0],"W":[17,27,21,21,17,17,17,0],"X":[17,17,10,4,10,17,17,0],"Y":[4,4,4,4,10,17,17,0],"Z":[31,16,8,4,2,1,31,0],"a":[15,17,15,1,14,0,0,0],"b":[14,17,17,25,22,16,16,0],"c":[14,17,16,16,14,0,0,0],"d":[15,17,17,19,13,1,1,0],"e":[14,16,31,17,14,0,0,0],"f":[4,4,4,14,4,5,2,0],"g":[14,1,15,17,15,0,0,0],"h":[17,17,17,25,22,16,16,0],"i":[14,4,4,4,12,0,4,0],"j":[12,18,2,2,2,0,2,0],"k":[9,10,12,10,9,8,8,0],"l":[14,4,4,4,4,4,12,0],"m":[21,21,21,21,26,0,0,0],"n":[17,17,17,25,22,0,0,0],"o":[14,17,17,17,14,0,0,0],"p":[16,16,30,17,30,0,0,0],"q":[1,1,15,17,15,0,0,0],"r":[8,8,8,12,11,0,0,0],"s":[30,1,14,16,15,0,0,0],"t":[2,5,4,4,14,4,0,0],"u":[13,19,17,17,17,0,0,0],"v":[4,10,17,17,17,0,0,0],"w":[10,21,21,17,17,0,0,0],"x":[19,12,4,6,25,0,0,0],"y":[24,4,6,9,17,0,0,0],"z":[31,8,4,2,31,0,0,0],"?":[4,0,4,2,1,17,14,0],",":[8,4,12,0,0,0,0,0],";":[8,4,12,0,12,12,0,0],":":[0,12,12,0,12,12,0,0],"|":[4,4,4,4,4,4,4,0],"@":[14,21,21,13,1,17,14,0],"%":[3,19,8,4,2,25,24,0],"[":[14,8,8,8,8,8,14,0],"&":[13,18,21,8,20,18,12,0],"_":[31,0,0,0,0,0,0,0],"'":[0,0,0,0,8,4,12,0],"]":[14,2,2,2,2,2,14,0],"\\":[0,1,2,4,8,16,0,0],"~":[0,0,0,22,13,0,0,0]};

var temperature = 29;
var humidity = 80;
var pressure = 1000;
var rotation = 0;
var flipH = false;
var flipV = false;

function updateTemperature(d) {
    temperature = Number(d.toFixed(1));
    document.getElementById('temperature').innerHTML = temperature.toFixed(1)+"&deg;C";
}
function changeTemperature(d) {
    updateTemperature(temperature+d);
    publishEnvironment();
}
function updateHumidity(d) {
    humidity = d;
    document.getElementById('humidity').innerHTML = humidity+"%";
}
function changeHumidity(d) {
    updateHumidity(humidity+d);
    publishEnvironment();
}
function updatePressure(d) {
    pressure = d;
    document.getElementById('pressure').innerHTML = pressure+"mb";
}
function changePressure(d) {
    updatePressure(pressure+d);
    publishEnvironment();
}
function publishEnvironment() {
    if (ws && ws.readyState === ws.OPEN) {
        ws.send("Y"+temperature+","+humidity+","+pressure);
    }
}

var k = document.getElementById("j-up");
k.onmousedown = function() { keyDown('U'); }
k.onmouseup = function() { keyUp('U'); }

k = document.getElementById("j-down");
k.onmousedown = function() { keyDown('D'); }
k.onmouseup = function() { keyUp('D'); }

k = document.getElementById("j-left");
k.onmousedown = function() { keyDown('L'); }
k.onmouseup = function() { keyUp('L'); }

k = document.getElementById("j-right");
k.onmousedown = function() { keyDown('R'); }
k.onmouseup = function() { keyUp('R'); }

k = document.getElementById("j-enter");
k.onmousedown = function() { keyDown('E'); }
k.onmouseup = function() { keyUp('E'); }

var keyTimer = null;
function keyDown(evt) {
    //  K[U|L|R|D|E][0|1|2] - joystick event:  direction,state
    ws.send("K"+evt+"1");
    keyTimer = setInterval(function() {
        ws.send("K"+evt+"2");
    },200);

}
function keyUp(evt) {
    //  K[U|L|R|D|E][0|1|2] - joystick event:  direction,state
    clearInterval(keyTimer);
    ws.send("K"+evt+"0");
}

var display = document.getElementById("display");
var cells = [];

for (var y=0;y<8;y++) {
    cells.push([]);
    var r = document.createElement("div");
    r.className = "row";
    display.appendChild(r);
    for (var x=0;x<8;x++) {
        var d = document.createElement("div");
        d.className = "led";
        d.id = "led_"+x+"_"+y;
        r.appendChild(d);
        cells[y].push(d);
    }
}

var ws;
function connect() {
    var location = document.location.toString().replace(/^http/,"ws")+"/ws";
    ws = new WebSocket(location);
    ws.onopen = function() {
        console.log("Connected to Node-RED")
    }
    ws.onclose = function() {
        console.log("Lost connection, reconnecting in 5 seconds")
        setTimeout(connect,5000);
    }
    ws.onmessage = function(msg) {
        var command = msg.data[0];
        var data = msg.data.substring(1);
        var parts;
        var x,y,t;
        if (command === 'Y') {
            parts = data.split(",");
            updateTemperature(Number(parts[0]));
            updateHumidity(Number(parts[1]));
            updatePressure(Number(parts[2]));
        } else if (command === 'D') {
            if (data === '0') {
                document.getElementById("display").classList.add("low_brightness");
            } else if (data === '1') {
                document.getElementById("display").classList.remove("low_brightness");
            }
        } else {
            clearTimeout(scrollMessageTimeout);
            if (command === 'P') {
                parts = data.split(",");
                for (var i=0;i<parts.length;i+=5) {
                    x = parts[i];
                    y = parts[i+1];
                    if (flipH) { x = 7-x; }
                    if (flipV) { y = 7-y; }
                    if (rotation === 180) {
                        x = 7-x;
                        y = 7-y;
                    } else if (rotation === 90) {
                        if (y<4 && x<4) { t = y; y = x; x = 7-t; }
                        else if (y<4 && x>3) { t=x; x = 7-y; y = t;  }
                        else if (y>3 && x>3) { t=x; x = 7-y; y = t;  }
                        else if (y>3 && x<4) { t=y; y = x; x = 7-t;  }
                    } else if (rotation === 270) {
                        if (y<4 && x<4) { t = x; x = y; y = 7-t; }
                        else if (y<4 && x>3) { t=y; y = 7-x; x = t;  }
                        else if (y>3 && x>3) { t=y; y = 7-x; x = t;  }
                        else if (y>3 && x<4) { t=x; x = y; y = 7-t;  }
                    }
                    cells[y][x].style.backgroundColor = "rgb("+parts[i+2]+","+parts[i+3]+","+parts[i+4]+")"
                }
            } else if (command === 'R') {
                var delta = Number(data) - rotation;
                if (delta < 0) {
                    delta += 360;
                }
                if (delta === 90) {
                    for (y=0;y<4;y++) {
                        for (x=0;x<4;x++) {
                            t = cells[y][x].style.backgroundColor;
                            cells[y][x].style.backgroundColor = cells[7-y][x].style.backgroundColor;
                            cells[7-y][x].style.backgroundColor = cells[7-x][7-y].style.backgroundColor;
                            cells[7-x][7-y].style.backgroundColor = cells[y][7-x].style.backgroundColor;
                            cells[y][7-x].style.backgroundColor = t;

                        }
                    }
                } else if (delta === 270) {
                    for (y=0;y<4;y++) {
                        for (x=0;x<4;x++) {
                            t = cells[y][x].style.backgroundColor;
                            cells[y][x].style.backgroundColor = cells[y][7-x].style.backgroundColor;
                            cells[y][7-x].style.backgroundColor = cells[7-x][7-y].style.backgroundColor;
                            cells[7-x][7-y].style.backgroundColor = cells[7-y][x].style.backgroundColor;
                            cells[7-y][x].style.backgroundColor = t;

                        }
                    }
                } else if (delta === 180) {
                    for (y=0;y<4;y++) {
                        for (x=0;x<4;x++) {
                            t = cells[y][x].style.backgroundColor;
                            cells[y][x].style.backgroundColor = cells[7-y][7-x].style.backgroundColor;
                            cells[7-y][7-x].style.backgroundColor = t;

                            t = cells[7-y][x].style.backgroundColor;
                            cells[7-y][x].style.backgroundColor = cells[y][7-x].style.backgroundColor;
                            cells[y][7-x].style.backgroundColor = t;

                        }
                    }
                }


                rotation = Number(data);
                //data = 0,90,180,270
            } else if (command === 'F') {
                if (data === 'H') {
                    flipH = !flipH;
                    for (y=0;y<8;y++) {
                        for (x=0;x<4;x++) {
                            t = cells[y][x].style.backgroundColor;
                            cells[y][x].style.backgroundColor = cells[y][7-x].style.backgroundColor;
                            cells[y][7-x].style.backgroundColor = t;
                        }
                    }
                } else {
                    flipV = !flipV;
                    for (x=0;x<8;x++) {
                        for (y=0;y<4;y++) {
                            t = cells[y][x].style.backgroundColor;
                            cells[y][x].style.backgroundColor = cells[7-y][x].style.backgroundColor;
                            cells[7-y][x].style.backgroundColor = t;
                        }
                    }
                }
            } else if (command === 'T'){
                var splitAt = data.indexOf(":");
                var colors = data.substring(0,splitAt);
                var message = data.substring(splitAt+1);
                var colorParts = colors.split(",");
                var foreground = "rgb("+colorParts[0]+","+colorParts[1]+","+colorParts[2]+")";
                var background = "rgb("+colorParts[3]+","+colorParts[4]+","+colorParts[5]+")";

                if (message.length === 1) {
                    drawCharAt(message[0],0,foreground,background)
                } else {
                    currentMessage = {
                        foreground: foreground,
                        background: background,
                        text: "  "+message +"  ",
                        speed: colors.length === 7?colors[6]:0.1,
                        pos: 0,
                        subpos: 0
                    }
                    stepMessage();
                }
            }
        }
    }
}

function drawCharAt(char,cx,foreground,background) {
    var charData = fontData[char]||fontData["?"];
    for (var y=0;y<8;y++) {
        var line = charData[y].toString(2);
        while(line.length < 5) {
            line = "0"+line;
        }
        for (var j=0;j<6;j++) {
            var x = cx + j;
            if (x >= 0 && x < 8) {
                if (j === 5) {
                    cells[7-y][x].style.backgroundColor = background;
                } else {
                    if (line[j] === "1") {
                        cells[7-y][x].style.backgroundColor = foreground;
                    } else {
                        cells[7-y][x].style.backgroundColor = background;
                    }
                }
            }
        }
    }
}

var currentMessage;
var scrollMessageTimeout;
function stepMessage() {
    var x = 0;
    var subpos = currentMessage.subpos;
    var colCount = 0;
    for (var i=currentMessage.pos;i<Math.min(currentMessage.pos+3,currentMessage.text.length);i++) {
        var char = currentMessage.text[i];
        drawCharAt(char,x-subpos,currentMessage.foreground,currentMessage.background)
        x += 5-subpos;
        subpos = 0;
    }
    currentMessage.subpos++;
    if (currentMessage.subpos === 5) {
        currentMessage.subpos = 0;
        currentMessage.pos++;
    }
    if (currentMessage.pos < currentMessage.text.length) {
        scrollMessageTimeout = setTimeout(stepMessage,currentMessage.speed*1000);
    }
}


connect();
</script>
</body>
</html>
